//
//  ViewController.m
//  21-NSURLResponseRequest
//
//  Created by Snow on 16/8/11.
//  Copyright © 2016年 Snow. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSURL *url = [NSURL URLWithString:@"http://www.baidu.com/1.html?userName=xxx&pwd=xxx"];
    NSLog(@"baidu:%@", url);
    //>1.完整的URL对应的字符串
    NSLog(@"baidu.absoluteString:%@", url.absoluteString);
    //>2.host:主机地址/域名
    NSLog(@"baidu.host:%@", url.host);
    //>3.path 主机地址 / 域名后对应的路径
    NSLog(@"baidu.path:%@", url.path);
    //>4.query ? 以后的内容
//    NSLog(@"baidu.parameterString:%@", url.parameterString);
    NSLog(@"baidu.query:%@", url.query);
    //>5.scheme URL所遵循的协议
    NSLog(@"baidu.scheme:%@", url.scheme);
    //>6.除了协议的部分
    NSLog(@"baidu.resourceSpecifier:%@", url.resourceSpecifier);

    //可变请求和不可变请求
    //   1. NSURLRequest 不可更改 只能获取
   NSURLRequest *baiduRequest = [NSURLRequest requestWithURL:url];
//       2. NSMutableURLRequest
    //一般处理网络请求 都是用NSMutableURLRequest
    NSMutableURLRequest *baiduMutableRequest = [NSMutableURLRequest requestWithURL:url];
    //1> 超时时间 一个请求持续多久 会失败
    baiduMutableRequest.timeoutInterval = 60;
    //2> 请求的姿势 GET post put delete
    baiduMutableRequest.HTTPMethod = @"GET";
    //3> H设置网络请求头部
    baiduMutableRequest.allHTTPHeaderFields = @{@"User-Agent": @"iOS10.0 / iPhone8, 1"};
    //根据字典特性 后填入值相同key 情况下, 会覆盖前面的值
    [baiduMutableRequest setValue:@"iOS10.0 / iPhone8, 1" forHTTPHeaderField:@"User-Agent"];
    [baiduMutableRequest valueForHTTPHeaderField:@"User-Agent"];
    NSLog(@"%@", baiduMutableRequest.allHTTPHeaderFields);
    //4> http body(一般POST请求时, 才会使用)
    //一般情况下 都是字典/字符串转换成Data
    baiduMutableRequest.HTTPBody = [NSData data];
    //response 响应
//    NSURLResponse
    NSHTTPURLResponse *response = [[NSHTTPURLResponse alloc] initWithURL:url statusCode:200 HTTPVersion:@"HTTP/1.1" headerFields:@{@"data": [NSData data]}];
    //1>statusCode 状态码
//    response.statusCode
    //响应头 内容格式 编码
//    2>response.allHeaderFields;
    //内容类型
    //Content-type : application/json;charset:utf8
    NSLog(@"%@", response.allHeaderFields);
    //时间 日期
    //缓存一些代码
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
